package wang.hu.bos.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import wang.hu.bos.domain.base.Courier;
import wang.hu.bos.service.CourierService;

import com.opensymphony.xwork2.ActionContext;

@Controller
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
public class CourierAction extends BaseAction<Courier> {
	@Autowired
	private CourierService courierService;

	@Action(value = "courier_delBatch", results = { @Result(name = "success", location = "./pages/base/courier.html", type = "redirect") })
	public String delBatch() {
		String[] ids = ServletActionContext.getRequest().getParameter("ids")
				.split(",");
		Character status = '1';
		courierService.delBatch(ids, status);
		return SUCCESS;
	}

	@Action(value = "courier_resBatch", results = { @Result(name = "success", location = "./pages/base/courier.html", type = "redirect") })
	public String resBatch() {
		String[] ids = ServletActionContext.getRequest().getParameter("ids")
				.split(",");
		Character status = '0';
		courierService.delBatch(ids, status);
		return SUCCESS;
	}

	@Action(value = "courier_save", results = { @Result(name = "success", location = "./pages/base/courier.html", type = "redirect") })
	public String save() {
		courierService.save(model);
		return SUCCESS;
	}

	@Action(value = "courier_pageData", results = { @Result(name = "success", type = "json") })
	public String pageData() {

		Pageable pageable = new PageRequest(page - 1, rows);

		Specification<Courier> specification = new Specification<Courier>() {
			List<Predicate> list = new ArrayList<Predicate>();

			@Override
			public Predicate toPredicate(Root<Courier> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				if (StringUtils.isNotBlank(model.getCourierNum())) {
					Predicate p1 = cb.equal(
							root.get("courierNum").as(String.class),
							model.getCourierNum());
					list.add(p1);
				}
				if (model.getStandard() != null
						&& StringUtils
								.isNotBlank(model.getStandard().getName())) {
					Join<Object, Object> join = root.join("standard",
							JoinType.INNER);
					Predicate p2 = cb.like(join.get("name").as(String.class),
							"%" + model.getStandard().getName() + "%");
					list.add(p2);
				}
				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p3 = cb.like(
							root.get("company").as(String.class),
							"%" + model.getCompany() + "%");
					list.add(p3);
				}
				if (StringUtils.isNotBlank(model.getType())) {
					Predicate p4 = cb.like(root.get("type").as(String.class),
							"%" + model.getType() + "%");
					list.add(p4);
				}

				return cb.and(list.toArray(new Predicate[0]));
			}

		};

		Page<Courier> result = courierService.findPageData(specification,
				pageable);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", result.getTotalElements());
		map.put("rows", result.getContent());
		ActionContext.getContext().getValueStack().push(map);

		return SUCCESS;
	}
}
